package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Slideshow;
import com.ws.lv.service.SlideshowService;
import com.ws.lv.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/slideshow")
public class SlideshowController {

    @Autowired
    SlideshowService slideshowService;

    @RequestMapping("/selectSlideshows")
    public String selectSlideshows(Model model, Integer slideshow_state, String order, Integer currentPage) {
        System.out.println("进入查询轮播图");
        model.addAttribute("slideshow_state", slideshow_state);
        model.addAttribute("order", order);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null) {
            pageHelper.startPage(1, 5);
        } else {
            pageHelper.startPage(currentPage, 5);
        }
        List<Slideshow> slideshows = slideshowService.selectSlideshows(slideshow_state, order);
        PageInfo pageInfo = new PageInfo(slideshows);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("currentPage", currentPage);
        System.out.println(slideshows);
        return "adminpage/slideshow/list";
    }

    //修改轮播图状态为显示
    @RequestMapping("/updateStateDisplay")
    public String updateStateDisplay(int slideshow_id) {
        System.out.println("进入修改轮播图状态为显示");
        int i = slideshowService.updateStateDisplay(slideshow_id);
        return "forward:/slideshow/selectSlideshows";
    }

    //修改轮播图状态为隐藏
    @RequestMapping("/updateStateHide")
    public String updateStateHide(int slideshow_id) {
        System.out.println("进入修改轮播图状态为隐藏");
        int i = slideshowService.updateStateHide(slideshow_id);
        return "forward:/slideshow/selectSlideshows";
    }

    //去修改轮播图页面
    @RequestMapping("/toUpdateSlideshow")
    public String toUpdateSlideshow(int slideshow_id, Model model) {
        System.out.println("进入去修改轮播图页面");
        Slideshow slideshow = slideshowService.selectSlideshowById(slideshow_id);
        model.addAttribute("slideshow", slideshow);
        return "adminpage/slideshow/update";
    }

    //修改轮播图
    @RequestMapping("/updateSlideshow")
    public String updateSlideshow(Slideshow slideshow, MultipartFile image) {
        System.out.println("修改轮播图");
        try {
            if (image.getOriginalFilename().length() > 0) {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String suffix = image.getOriginalFilename().split("\\.")[1];
                //删除原图片
                File file = new File(Res.fileDir + slideshow.getSlideshow_url());
                if (file.exists()) {
                    file.delete();
                    System.out.println("删除原轮播图片");
                }
                slideshow.setSlideshow_url(uuid + "." + suffix);
                image.transferTo(new File(Res.fileDir + uuid + "." + suffix));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = slideshowService.updateSlideshowById(slideshow);
        return "redirect:/slideshow/selectSlideshows";
    }

}
